﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelOS_Definition : BaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-OS.xml";

        #endregion Fields

        #region Constructors

        public StifelOS_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelOS_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelOS c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelOS>( ),
                ContentTypeId = Item.GetContentTypeId<StifelOS>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes an operating system.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.Active))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.Default))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftCPU)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftHardDriveLabel)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftHardDriveName)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftHardDrives)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftHDSizeGB)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftMemoryGB)),
                        Required=true
                    },
                    //new FieldDefn{
                    //    Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.DftNetworkConnections)),
                    //    Required=true
                    //},
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.InternalOSName)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.LegacyOS)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.MaxCPU)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.MaxHardDrives)),
                        Required=true
                    },
                    //new FieldDefn{
                    //    Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.MaxHDSizeGB)),
                    //    Required=true
                    //},
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.MaxMemoryGB)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.MaxNetworkConnections)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.OSName))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.RolesId))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelOS>(c.GetPropertySymbol(o=>o.Server))
                    }
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelOSDao.ListName,
                Title = "Stifel OS",
                Description = "Contains the server operating systems.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelOS>()
                }.ToList( )
            };
        }

        List<int?> getRoles( )
        {
            List<int?> roles = new List<int?>( );
            StifelOSRoleDao dao = new StifelOSRoleDao( );
            dao.GetAll( ).ForEach( a =>
            {
                if ( a.Id.HasValue )
                {
                    roles.Add( a.Id );
                }
            } );
            return roles;
        }

        void loadList( )
        {
            StifelOSDao dao = new StifelOSDao( );
            dao.Clear( );
            dao.Add( new StifelOS
            {
                Title = "Windows 2003 R2 64bit",
                Active = true,
                Default = false,
                DftCPU = 1,
                DftHardDriveName = "C",
                DftHardDriveLabel = "OS",
                DftHardDrives = 1,
                DftHDSizeGB = 256,
                DftMemoryGB = 4,
                //DftNetworkConnections = 1,
                InternalOSName = "",
                MaxCPU = 8,
                MaxHardDrives = 3,
                //MaxHDSizeGB = 1024,
                MaxMemoryGB = 32,
                MaxNetworkConnections = 4,
                RolesId = getRoles( ),
                Server = true
            } );
            dao.Add( new StifelOS
            {
                Title = "Windows 2008 R2 Standard",
                Active = true,
                Default = true,
                DftCPU = 1,
                DftHardDriveName = "C",
                DftHardDriveLabel = "OS",
                DftHardDrives = 1,
                DftHDSizeGB = 256,
                DftMemoryGB = 4,
                //DftNetworkConnections = 1,
                InternalOSName = "",
                MaxCPU = 8,
                MaxHardDrives = 3,
                //MaxHDSizeGB = 1024,
                MaxMemoryGB = 32,
                MaxNetworkConnections = 4,
                RolesId = getRoles( ),
                Server = true
            } );
        }

        #endregion Methods
    }
}